package com.huanling.main.utils;

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;

import java.util.Objects;
import java.util.Optional;

/**
 * @author guibo.sun
 * @since 23 七月 2021
 */
public class AlertUtil {

    private AlertUtil() {
    }

    /**
     * 简易生成alert
     *
     * @param alertType   类型
     * @param title       题目
     * @param headerText  header描述
     * @param contentText 正文描述
     */
    public static void showAlert(Alert.AlertType alertType, String title, String headerText, String contentText) {
        create().withAlertType(alertType)
                .withContentText(contentText)
                .withHeaderText(headerText)
                .withTitle(title)
                .buildAndShow();
    }

    public static AlertBuilder create() {
        return new AlertBuilder();
    }


    public static class AlertBuilder {
        private AlertType alertType;
        private String title;
        private String headerText;
        private String contentText;

        private AlertBuilder() {
        }

        public AlertBuilder withAlertType(Alert.AlertType alertType) {
            this.alertType = alertType;
            return this;
        }

        public AlertBuilder withTitle(String title) {
            this.title = title;
            return this;
        }

        public AlertBuilder withHeaderText(String headerText) {
            this.headerText = headerText;
            return this;
        }

        public AlertBuilder withContentText(String contentText) {
            this.contentText = contentText;
            return this;
        }

        public void buildAndShow() {
            Objects.requireNonNull(alertType, "alert Type为空！");
            Alert alert = new Alert(alertType);
            alert.setContentText(contentText);
            alert.setHeaderText(headerText);
            alert.setTitle(title);
            alert.show();
        }


        /**
         * 阻塞式弹框提示
         *
         * @return 用户点击的button
         */
        public Optional<ButtonType> buildAndShowWait() {
            Objects.requireNonNull(alertType, "alert Type为空！");
            Alert alert = new Alert(alertType);
            alert.setContentText(contentText);
            alert.setHeaderText(headerText);
            alert.setTitle(title);
            return alert.showAndWait();
        }
    }


}
